package com.ctg.itrdc.sysmgr.portal.service.aop.strategy.delete.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.ctg.itrdc.sysmgr.portal.api.dto.FuncCompDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.FuncMenuDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.OrgPostRelDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.OrganizationDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.PrivFuncRelDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.PrivGrantDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.PrivilegeDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.StaffDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.StaffOrgRelDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemPostDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemPostRoleDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemRolesDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserPostDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserRoleDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.IOrgPostRelService;
import com.ctg.itrdc.sysmgr.portal.api.service.IPrivFuncRelService;
import com.ctg.itrdc.sysmgr.portal.api.service.IPrivGrantService;
import com.ctg.itrdc.sysmgr.portal.api.service.IPrivilegeService;
import com.ctg.itrdc.sysmgr.portal.api.service.IStaffOrgRelService;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemPostRoleService;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemUserPostService;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemUserRoleService;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemUserService;
import com.ctg.itrdc.sysmgr.portal.common.constant.PrivGrantEnum.GrantSysobjType;
import com.ctg.itrdc.sysmgr.portal.common.constant.PrivGrantEnum.PrivRefType;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.delete.IDeleteStrategy;

@Service("deleteStrategy")
public class DeleteStrategyImpl implements IDeleteStrategy {

	@Resource
	private ISystemUserPostService systemUserPostService;
	@Resource
	private ISystemUserRoleService systemUserRoleService;
	@Resource
	private ISystemPostRoleService systemPostRoleService;
	@Resource
	private IStaffOrgRelService staffOrgRelService;
	@Resource
	private ISystemUserService systemUserService;
	@Resource
	private IOrgPostRelService orgPostRelService;
	@Resource
	private IPrivFuncRelService privFuncRelService;
	@Resource
	private IPrivilegeService privilegeService;
	@Resource
	private IPrivGrantService privGrantService;

	@Override
	public void deleteSystemUserRel(SystemUserDTO dto) {
		
		Long sysUserId = dto.getSysUserId();
		if(sysUserId==null) return;
		
		SystemUserPostDTO sup = new SystemUserPostDTO();
		sup.setSysUserId(sysUserId);
//		systemUserPostService.asynBatchDeleteByExample(sup);
		List<SystemUserPostDTO> supList = systemUserPostService.selectByExample(sup);
		if(!CollectionUtils.isEmpty(supList)) {
			systemUserPostService.batchDeleteByPrimaryKey(supList);
		}

		SystemUserRoleDTO sur = new SystemUserRoleDTO();
		sur.setSysUserId(sysUserId);
//		systemUserRoleService.asynBatchDeleteByExample(sur);
		List<SystemUserRoleDTO> surList = systemUserRoleService.selectByExample(sur);
		if(!CollectionUtils.isEmpty(surList)) {
			systemUserRoleService.batchDeleteByPrimaryKey(surList);
		}
		
		PrivGrantDTO pg = new PrivGrantDTO();
		pg.setGrantObjId(sysUserId);
		pg.setGrantObjType(GrantSysobjType.SYSTEM_USER.toString());
//		privGrantService.asynBatchDeleteByExample(pg);
		List<PrivGrantDTO> pgList = privGrantService.selectByExample(pg);
		if(!CollectionUtils.isEmpty(pgList)) {
			privGrantService.batchDeleteByPrimaryKey(pgList);
		}
	}

	@Override
	public void deleteSystemPostRel(SystemPostDTO dto) {
		
		Long sysPostId = dto.getSysPostId();
		if(sysPostId==null) return;
		
		SystemUserPostDTO sup = new SystemUserPostDTO();
		sup.setSysPostId(sysPostId);
//		systemUserPostService.asynBatchDeleteByExample(sup);
		List<SystemUserPostDTO> supList = systemUserPostService.selectByExample(sup);
		if(!CollectionUtils.isEmpty(supList)) {
			systemUserPostService.batchDeleteByPrimaryKey(supList);
		}

		SystemPostRoleDTO spr = new SystemPostRoleDTO();
		spr.setSysPostId(sysPostId);
//		systemPostRoleService.asynBatchDeleteByExample(spr);
		List<SystemPostRoleDTO> sprList = systemPostRoleService.selectByExample(spr);
		if(!CollectionUtils.isEmpty(sprList)) {
			systemPostRoleService.batchDeleteByPrimaryKey(sprList);
		}
		
		PrivGrantDTO pg = new PrivGrantDTO();
		pg.setGrantObjId(sysPostId);
		pg.setGrantObjType(GrantSysobjType.SYSTEM_POST.toString());
//		privGrantService.asynBatchDeleteByExample(pg);
		List<PrivGrantDTO> pgList = privGrantService.selectByExample(pg);
		if(!CollectionUtils.isEmpty(pgList)) {
			privGrantService.batchDeleteByPrimaryKey(pgList);
		}
	}

	@Override
	public void deleteSystemRolesRel(SystemRolesDTO dto) {
		
		Long sysRoleId = dto.getSysRoleId();
		if(sysRoleId==null) return;
		
		SystemUserRoleDTO sur = new SystemUserRoleDTO();
		sur.setSysRoleId(sysRoleId);
//		systemUserRoleService.asynBatchDeleteByExample(sur);
		List<SystemUserRoleDTO> surList = systemUserRoleService.selectByExample(sur);
		if(!CollectionUtils.isEmpty(surList)) {
			systemUserRoleService.batchDeleteByPrimaryKey(surList);
		}
		
		SystemPostRoleDTO spr = new SystemPostRoleDTO();
		spr.setSysRoleId(sysRoleId);
//		systemPostRoleService.asynBatchDeleteByExample(spr);
		List<SystemPostRoleDTO> sprList = systemPostRoleService.selectByExample(spr);
		if(!CollectionUtils.isEmpty(sprList)) {
			systemPostRoleService.batchDeleteByPrimaryKey(sprList);
		}

		PrivGrantDTO pg = new PrivGrantDTO();
		pg.setGrantObjId(sysRoleId);
		pg.setGrantObjType(GrantSysobjType.SYSTEM_ROLE.toString());
//		privGrantService.asynBatchDeleteByExample(pg);
		List<PrivGrantDTO> pgList = privGrantService.selectByExample(pg);
		if(!CollectionUtils.isEmpty(pgList)) {
			privGrantService.batchDeleteByPrimaryKey(pgList);
		}
	}

	@Override
	public void deletePrivilegeRel(PrivilegeDTO dto) {
		Long privId = dto.getPrivId();
		if(privId==null) return;
		
		PrivFuncRelDTO pfr = new PrivFuncRelDTO();
		pfr.setPrivId(privId);
//		privFuncRelService.asynBatchDeleteByExample(pfr);
		List<PrivFuncRelDTO> pfrList = privFuncRelService.selectByExample(pfr);
		if(!CollectionUtils.isEmpty(pfrList)) {
			privFuncRelService.batchDeleteByPrimaryKey(pfrList);
		}
		
		PrivGrantDTO pg = new PrivGrantDTO();
		pg.setPrivId(privId);
//		privGrantService.asynBatchDeleteByExample(pg);
		List<PrivGrantDTO> pgList = privGrantService.selectByExample(pg);
		if(!CollectionUtils.isEmpty(pgList)) {
			privGrantService.batchDeleteByPrimaryKey(pgList);
		}
		
	}

	@Override
	public void deleteFuncMenuRel(FuncMenuDTO dto) {
		Long menuId = dto.getMenuId();
		if(menuId==null) return ;
		
		PrivFuncRelDTO pfr = new PrivFuncRelDTO();
		pfr.setPrivRefId(menuId.longValue());
		pfr.setPrivRefType(PrivRefType.MENU.toString());
//		privFuncRelService.asynBatchDeleteByExample(pfr);
		List<PrivFuncRelDTO> pfrList = privFuncRelService.selectByExample(pfr);
		List<PrivilegeDTO> privilegeList = new ArrayList<PrivilegeDTO>();
		for(PrivFuncRelDTO pfrDTO : pfrList) {
			Long privId = pfrDTO.getPrivId();
			PrivilegeDTO privilege = new PrivilegeDTO();
			privilege.setPrivId(privId);
			privilegeList.add(privilege);
		}
		if(!CollectionUtils.isEmpty(privilegeList)) {
			privilegeService.batchDeleteByPrimaryKey(privilegeList);
		}
	}

	@Override
	public void deleteFuncCompRel(FuncCompDTO dto) {
		Long compId = dto.getCompId();
		if(compId==null) return ;
		
		PrivFuncRelDTO pfr = new PrivFuncRelDTO();
		pfr.setPrivRefId(compId.longValue());
		pfr.setPrivRefType(PrivRefType.COMP.toString());
//		privFuncRelService.asynBatchDeleteByExample(pfr);
		List<PrivFuncRelDTO> pfrList = privFuncRelService.selectByExample(pfr);
		List<PrivilegeDTO> privilegeList = new ArrayList<PrivilegeDTO>();
		for(PrivFuncRelDTO pfrDTO : pfrList) {
			Long privId = pfrDTO.getPrivId();
			PrivilegeDTO privilege = new PrivilegeDTO();
			privilege.setPrivId(privId);
			privilegeList.add(privilege);
		}
		if(!CollectionUtils.isEmpty(privilegeList)) {
			privilegeService.batchDeleteByPrimaryKey(privilegeList);
		}
	}

	@Override
	public void deleteStaffRel(StaffDTO dto) {
		Long staffId = dto.getStaffId();
		if(staffId==null) return;
		
		StaffOrgRelDTO sor = new StaffOrgRelDTO();
		sor.setStaffId(staffId);
//		staffOrgRelService.asynBatchDeleteByExample(sor);
		List<StaffOrgRelDTO> sorList = staffOrgRelService.selectByExample(sor);
		if(!CollectionUtils.isEmpty(sorList)) {
			staffOrgRelService.batchDeleteByPrimaryKey(sorList);
		}
	}
}
